package mmc.resource;

import static org.lwjgl.opengl.GL11.*;

import java.awt.Dimension;

import mmc.Client;

import org.lwjgl.opengl.GL11;

public class Text12p {
	
	public static void glText12p(String text, float x, float y){
		Alphabet12p tex = Alphabet12p.getAlphabetTexture();
		Dimension size = Client.client.getSize();
		
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();
		glOrtho(0, (int)size.width, (int)size.height, 0, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();
		
		glEnable(GL_TEXTURE_2D);
		tex.bind();
		
		int screenx = (int) (x*size.width);
		int screeny = (int) (y*size.height);
		
		GL11.glBegin(GL11.GL_QUADS);
		for(char ch : text.toCharArray()){
			Integer position = Alphabet12p.charPositions.get(ch);
			if(position==null)
				position=0;
			
			int posx=position;
			int posy=posx>>Integer.SIZE/2;
			posx &= (1<<Integer.SIZE/2)-1;
			
			float posx2 = posx + Alphabet12p.CHAR_WIDTH,
			      posy2 = posy + Alphabet12p.CHAR_HEIGHT;
			float posx1 = posx,
			      posy1 = posy;
			
			posx1 /= Alphabet12p.WIDTH;
			posx2 /= Alphabet12p.WIDTH;
			posy1 /= Alphabet12p.HEIGHT;
			posy2 /= Alphabet12p.HEIGHT;
			
			glTexCoord2f(posx1, posy1);
			GL11.glVertex2f(screenx, screeny);
			glTexCoord2f(posx2, posy1);
			GL11.glVertex2f(screenx+Alphabet12p.CHAR_WIDTH, screeny);
			glTexCoord2f(posx2, posy2);
			GL11.glVertex2f(screenx+Alphabet12p.CHAR_WIDTH, screeny+Alphabet12p.CHAR_HEIGHT);
			glTexCoord2f(posx1, posy2);
			GL11.glVertex2f(screenx, screeny+Alphabet12p.CHAR_HEIGHT);
			screenx+=Alphabet12p.CHAR_WIDTH;
		}
		GL11.glEnd();
		glDisable(GL_TEXTURE_2D);
		
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glPopMatrix();
	}
}
